Conditional Compilation
Conditional compilation allows you to control the inclusion or exclusion of code blocks based on specific conditions before the script is executed. This is helpful for things like platform-specific code, debugging, or environment-dependent settings, allowing you to optimize your project dynamically.
By using preprocessor directives, you can conditionally include or exclude parts of your code based on conditions evaluated during the compile time.
The following preprocessor directives are available for conditional compilation:
Directive | Description |
---|---|
#define | Defines a macro or flag to be used in conditions. |
#ifdef | Checks whether a macro or flag has been defined. |
#ifndef | Checks whether a macro or flag has not been defined. |
#endif | Ends a conditional block. |
#elif | Used for an "else if" condition in a chain of checks. |
#else | Specifies an alternative code block when the condition is false. |
These directives help manage code flow based on predefined flags or conditions, allowing you to adapt the script depending on the environment or build settings.
Example:
You can define a flag at the top of your script and then conditionally execute different code blocks depending on whether the flag is defined.
#define DEVELOPMENT
#ifdef DEVELOPMENT
// This block is included if DEVELOPMENT is defined
console.log("Development mode enabled.");
#else
console.log("Production mode enabled.");
// Production-specific code here
#endif
Predefined Constants
In addition to defining your own flags using #define
, the scripting runtime comes with some predefined constants that can be used to automatically detect the platform or runtime environment during compile time. These constants help you easily target specific platforms or environments (such as mobile, desktop, or the editor), without needing to manually define the conditions yourself.
Predefined Constants Available
Constant | Description |
---|---|
PLATFORM_ANDROID | This constant is automatically defined if the code is running on an Android device. |
PLATFORM_IOS | This constant is automatically defined if the code is running on an iOS device. |
PLATFORM_MACOS | This constant is automatically defined if the code is running on a macOS device. |
PLATFORM_WINDOWS | This constant is automatically defined if the code is running on a Windows device. |
PLATFORM_LINUX | This constant is automatically defined if the code is running on a Linux device. |
IS_MOBILE | This constant is defined if the platform is a mobile device (i.e., either Android or iOS). |
EDITOR | This constant is defined when the code is running inside the XR Creator Editor (i.e., during development or testing in the editor environment). |
DEBUG | This constant is defined when the script is running in debug mode. It’s useful for enabling debug-specific code, such as logging and error handling, that should only appear in a development environment. |
These predefined constants are automatically set by the runtime and provide useful information about the environment in which the code is running. Using these constants, you can write platform-specific code that is included only when running in the appropriate environment.